#!/usr/bin/env python

from pymesh import trigen
import matplotlib.pyplot as plt

def plot_segments(m):
    for i in range(0, len(m.segments), 2):
        n1 = m.segments[i  ]-1
        n2 = m.segments[i+1]-1
        print n1, n2,
        x = [m.points[2*n1],   m.points[2*n2]]
        y = [m.points[2*n1+1], m.points[2*n2+1]]
        print [x[0], y[0]], [x[1], y[1]]
        plt.plot(x, y, "k-o")
    plt.show()

def plot_triangles(m):
    for i in range(0, len(m.triangles), 3):
        n1 = m.triangles[i  ]-1
        n2 = m.triangles[i+1]-1
        n3 = m.triangles[i+2]-1
        x = [m.points[2*n1],   m.points[2*n2],   m.points[2*n3],   m.points[2*n1]  ]
        y = [m.points[2*n1+1], m.points[2*n2+1], m.points[2*n3+1], m.points[2*n1+1]]
        plt.plot(x, y, "k-o")
    plt.show()

    

a = trigen.MeshIO()
a.points = [0.200000, -0.776400,
            0.220000, -0.773200,
            0.245600, -0.756400,
            0.277600, -0.702000,
            0.488800, -0.207600,
            0.504800, -0.207600,
            0.740800, -0.739600,
            0.756000, -0.761200,
            0.774400, -0.772400,
            0.800000, -0.776400,
            0.800000, -0.792400,
            0.579200, -0.792400,
            0.579200, -0.776400,
            0.621600, -0.771600,
            0.633600, -0.762800,
            0.639200, -0.744400,
            0.620800, -0.684400,
            0.587200, -0.604400,
            0.360800, -0.604400,
            0.319200, -0.706800,
            0.312000, -0.739600,
            0.318400, -0.761200,
            0.334400, -0.771600,
            0.371200, -0.776400,
            0.371200, -0.792400,
            0.374400, -0.570000,
            0.574400, -0.570000,
            0.473600, -0.330800,
            0.200000, -0.792400]
a.segments = [   29, 1,
                 1,  2,
                 2,  3,
                 3,  4,
                 4,  5,
                 5,  6,
                 6,  7,
                 7,  8,
                 8,  9,
                 9, 10,
                10, 11,
                11, 12,
                12, 13,
                13, 14,
                14, 15,
                15, 16,
                16, 17,
                17, 18,
                18, 19,
                19, 20,
                20, 21,
                21, 22,
                22, 23,
                23, 24,
                24, 25,
                25, 29,
                26, 27,
                27, 28,
                28, 26]
a.holes = [0.47, -0.5]
b =trigen.MeshIO()

trigen.mesh("pq", a, b)
# print b.triangles
plot_triangles(b)

trigen.vtk("test.vtk", b)

